package com.restaurant.web.android.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.restaurant.domain.Menu;
import com.restaurant.service.MenuService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Component
@RequestMapping("/sidedishmenuservice.do")
public class SidedishMenuController {

	@Autowired
	private MenuService objMenuService;
	
	@RequestMapping(method=RequestMethod.POST)
	public void sidesidhMenuControllerPOSTRequest(HttpServletRequest request, HttpServletResponse response){
		String menuID = request.getParameter("menuID");
		
		//Set the Menu list which are selected menu side dishes
		String queryLoadMenuSidedish = "select distinct m from Menu as m";
		queryLoadMenuSidedish += " where m.menuID in";
		queryLoadMenuSidedish += " (select distinct md.menu.menuID from Sidedishmenu as md where md.menu1.menuID = " + menuID ;
		queryLoadMenuSidedish += ") and m.menuID != " + menuID ;
		
		ArrayList<Menu> listMenuSidedish = null;
		try {
			listMenuSidedish = (ArrayList<Menu>) objMenuService.loadByHql(queryLoadMenuSidedish);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		XStream objXStream = new XStream(new DomDriver());
		objXStream.alias("menu", Menu.class);
		objXStream.alias("list", List.class);
		
		try {
			response.getWriter().print(objXStream.toXML(listMenuSidedish));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
